package selfview.sufutian.com.myapplication.draw2d;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import selfview.sufutian.com.myapplication.R;

/**
 * Created by sufutian on 2017/2/6.
 */

public class BitmapDraw extends View {

    private int mTotalWidth, mTotalHeight;

    private Bitmap bitmap;

    private Paint mPaint;

    private Rect srcRect, descRect;
    private int bitmapWidth;
    private int bitmapHeight;


    public BitmapDraw(Context context) {
        super(context);
        initBitmap();
        initPaint();
    }

    public BitmapDraw(Context context, AttributeSet attrs) {
        super(context, attrs);
        initBitmap();
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setFilterBitmap(true);//防锯齿
        mPaint.setDither(true);//防止抖动
    }

    public BitmapDraw(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initBitmap() {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) (getResources().getDrawable(R.drawable.image_2));
        bitmap = bitmapDrawable.getBitmap();
        bitmapWidth = bitmap.getWidth();
        bitmapHeight = bitmap.getHeight();
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTotalWidth = w;
        mTotalHeight = h;
    }

    boolean isFirst = true;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        srcRect = new Rect(0, 0, bitmapWidth, bitmapHeight);

//        中间位置
//        int left=mTotalWidth/2-bitmapWidth/2;
//        int top=mTotalHeight/2-bitmapHeight/2;
//        descRect=new Rect(left,top,left+bitmapWidth,top+bitmapHeight);

//        descRect = new Rect(0, 0, bitmapWidth, bitmapHeight);

        if (descRect != null) {
            canvas.drawBitmap(bitmap, srcRect, descRect, mPaint);
        }


    }


    private int mStartLeft, mStartTop, mToLeft, mToTop;


    public void startTranslate() {
//        startTranslate(0, 0, 900, 200, 3000);
        startTranslate2(0, 0, 900, 200, 3000);
    }

    public void startTranslate(int startLeft, int startTop, int toLeft, int toTop, long duration) {
        mStartLeft = startLeft;
        mStartTop = startTop;
        mToLeft = toLeft;
        mToTop = toTop;

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.setDuration(duration);
        valueAnimator.setInterpolator(new AccelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float fraction = (Float) animation.getAnimatedValue();

                int currentLeft = (int) ((mToLeft - mStartLeft) * fraction + mStartLeft);
                int currentTop = (int) ((mToTop - mStartTop) * fraction + mStartTop);

                if (descRect == null) {
                    descRect = new Rect(currentLeft, currentTop, currentLeft + bitmapWidth,
                            currentTop + bitmapHeight);
                }

                descRect.left = currentLeft;
                descRect.right = currentLeft + bitmapWidth;
                descRect.top = currentTop;
                descRect.bottom = currentTop + bitmapHeight;
                // 重绘
                postInvalidate();
            }
        });

        valueAnimator.start();
    }

    public void startTranslate2(int startLeft, int startTop, int toLeft, int toTop, long duration) {
        mStartLeft = startLeft;
        mStartTop = startTop;
        mToLeft = toLeft;
        mToTop = toTop;

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(1, 0);
        valueAnimator.setDuration(duration);
        valueAnimator.setInterpolator(new AccelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float fraction = (Float) animation.getAnimatedValue();

                if (descRect == null) {
                    descRect = new Rect(0, 0, bitmapWidth,
                            bitmapHeight);
                }
                descRect.right = (int) (fraction * bitmapWidth);
                // 重绘
                postInvalidate();
            }
        });

        valueAnimator.start();
    }


}
